<?php


//包含公共文件
include '../../common/common.php';


// var_dump($_GET);

//判断是否已经登录
if (!$_COOKIE['afterlogin']) {

	exit('请先登录再进行购买<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
}


//接收get传值
$id   = $_GET['id'];
$uid = $_COOKIE['uid'];
// var_dump($id);
// var_dump($uid);



//判断买家是否已经购买当前帖子
$resBuy = mySelect($link , '*' ,DB_TABLE_ORDER , "where tid = $id  and  uid = $uid");

//若未购买, 则进行购买操作
if (!$resBuy) {

	//获取帖子的价格rate,作者authorid ,
	$resPrice = mySelect($link , '*' , DB_TABLE_DETAIL , "where id = $id");
	$rate = $resPrice[0]['rate'];
	$authorid = $resPrice[0]['authorid'];

	// var_dump($resPrice);
	// var_dump($rate);
	// var_dump($authorid);

	//判断是否为帖子的主人
	if ($uid != $authorid) {
	
		//修改买卖双方的积分,和等级
		//买家积分减去价格,
		$resBuy = mySelect($link , '*' ,DB_TABLE_USER , "where uid = $uid");
		$buyGrade = $resBuy[0]['grade'];
		// var_dump($resBuy);
		// var_dump($buyGrade);

		//判断买家积分是否足够购买当前帖子
		if ($buyGrade < $rate) {

			exit('金币不足,仅剩<span style="color:red;">'. $buyGrade .' </span>积分,无法购买当前帖子<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
		}

		//计算积分,并获取最新等级信息
		$buy         = $buyGrade - $rate;
		$grade1     = myLevel($buy);
		$grade2     = $grade1[0];
		$levName1 = $grade1[1];

		//修改买家用户信息
		$resBuy2   = myUpdate($link , DB_TABLE_USER , ['grade' => $buy, 'levname' => "$levName1"] , "uid = $uid");
		// var_dump($buy);
		// var_dump($resBuy2);

		//判断付款是否成功
		if (!$resBuy2) {
			exit('付款失败<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
		}
	/*	//修改买家在detail的购买状态
		$resIsBuy = myUpdate($link , DB_TABLE_DETAIL , ['isbuy' => 1] , "id=$id");
		// var_dump($resIsBuy);*/



		//查询卖家积分,并给积分加上价格
		$resSell    = mySelect($link , '*' ,DB_TABLE_USER , "where uid = $authorid");
		$sellGrade = $resSell[0]['grade'];

		//计算积分,并获取最新等级信息
		$sell         = $sellGrade + $rate;
		$grade3     = myLevel($sell);
		$grade4     = $grade3[0];
		$levName2 = $grade3[1];
		
		//修改卖家用户信息
		$resSell2   = myUpdate($link , DB_TABLE_USER , ['grade' => $sell , 'levname' => "$levName2"] , " uid = $authorid");
		// var_dump($resSell);
		// var_dump($sellGrade);
		// var_dump($sell);
		// var_dump($resSell2);


		//判断收款是否成功 ,若不成功,则还原买家积分  [类似回滚]
		if (!$resSell2) {

			 //echo 22222222222;
			//还原买家积分,并重新获取最新等级信息
			$grade12     = myLevel($buyGrade);
			$grade22     = $grade12[0];
			$levName12 = $grade12[1];

			//还原买家用户信息
			$resBuy2   = myUpdate($link , DB_TABLE_USER , ['grade' => $buyGrade, 'levname' => "$levName12"] , "uid = $uid");

			exit('收款失败<br /> <a href="'  . $_SERVER['HTTP_REFERER'] .'">返回上一页</a>');
		}


		//双方交易成功后, 再插入订单数据 (暂未使用回滚)
		//添加订单数据
		$data = [
			'uid'        => $uid,
			'tid'     	   => $id,
			'rate'   	   => $rate,
			'ispay'     => 1,
			'addtime' => time()
		];

		$resPay = myInsert($link ,  DB_TABLE_ORDER , $data);

		// var_dump($data);
		// var_dump($resPay);


		//页面跳转
		echo '亲爱的用户<span style="color:red;"> ' . $_COOKIE['username'] .'</span>您已成功购买帖子,已花费<span style="color:red;"> ' . $rate.' </span>积分,当前等级为<span style="color:red;"> ' . $levName1 .'</span>,';

	} else {

		echo '您好主人,';
	}
}


//跳转页面
echo '欢迎查看';
header('refresh:1;url=' . $_SERVER['HTTP_REFERER']);
